package main
import "fmt"
import "sync"
import "runtime"
var counter int = 0
func Count(lock *sync.Mutex) {
	lock.Lock()
	counter++
	fmt.Println("counter = ",counter)
	lock.Unlock()
}
func main() {
	lock := &sync.Mutex{}
	for i := 0; i < 10; i++ {
		go Count(lock)
	}
	for {
		lock.Lock()
		c := counter
		fmt.Println("c= ",c)//主函数里打印的同时，goroutine里其实也在同时并发10个打印
		lock.Unlock()
		runtime.Gosched()//用于让出CPU时间片，让出当前goroutine的执行权限，调度器安排其它等待的任务运行，并在下次某个时候从该位置恢复执行
		if c >= 10 {
			break
		}
	}
}